package com.sth.edu.controller;


import com.sth.base.exceptionHandler.SthException;
import com.sth.commonutils.R;
import com.sth.edu.client.VodClient;
import com.sth.edu.entity.Video;
import com.sth.edu.service.VideoService;
import com.sth.edu.utils.UUIDUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author sth
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
@Api(description = "课程大纲")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody Video video) {
        video.setId(UUIDUtil.get4UUID());
        videoService.save(video);
        return R.ok();
    }

    //删除小节
    //TODO 后面需要完善.同时要删除视频
    @DeleteMapping("{id}")
    public R deleteVidio(@PathVariable String id) {


        // 根据小节id ->找到视频id
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeAlyVideo(videoSourceId);
            if(result.getCode()==20001){
                throw new SthException(20001,"删除视频失败");
            }
        }
        // 注意顺序
        videoService.removeById(id);
        return R.ok();
    }

    // 修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody Video video) {
        videoService.updateById(video);
        return R.ok();
    }
}

